package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.CryptoUtil;
import com.zhongkai.app.service.AnomalyListService;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Anomaly;
import com.zhongkai.model3.Guard;
import com.zhongkai.model3.SetUnit;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 维修任务列表
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AnomalyListServiceImpl implements AnomalyListService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;


    @Autowired
    private JedisClient jedisClient;

    /***
     * 维修用户任务列表
     * @param wx_uuid 维修用户uuid
     *
     * */
    @Override
    public ZhongkaiResult anomalyList(String wx_uuid, Integer page, Integer rows) {
        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Anomaly.class);
        detachedCriteria.add(Restrictions.eq("getUuid", wx_uuid));
        detachedCriteria.add(Restrictions.eq("orders", "1"));  //1 已派单 2未派单
/*        detachedCriteria.add(Restrictions.eq("sellRemove", "1")); //增加条件
        detachedCriteria.addOrder(Order.desc("sellTime")); //根据传入的字段进行降序排序*/
        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);
        return ZhongkaiResult.ok(pageBean);


    }

    /***
     * 添加历史
     * @param wx_uuid 维修用户uuid
     *
     * */
    @Override
    public ZhongkaiResult userMangeList(String wx_uuid, Integer page, Integer rows) {

        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SetUnit.class);
        detachedCriteria.add(Restrictions.eq("opeUuid", "36282293354442634856283"));
        // detachedCriteria.add(Restrictions.eq("opeUuid", wx_uuid));
/*        detachedCriteria.add(Restrictions.eq("sellRemove", "1")); //增加条件
        detachedCriteria.addOrder(Order.desc("sellTime")); //根据传入的字段进行降序排序*/
        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);
        return ZhongkaiResult.ok(pageBean);
    }


    /***
     * 修改签名
     * @param wx_uuid 维修用户uuid
     * @param sign 新签名
     *
     * */
    @Override
    public ZhongkaiResult updataSign(String wx_uuid, String sign) throws Exception {
        if (StringUtils.isNullOrEmpty(sign) || StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }

        guard.setGuaSigna(sign);

        iBaseDao.update(guard);
        return ZhongkaiResult.ok();
    }


    /***
     * 修改签名
     * @param wx_uuid 维修用户uuid
     * @param name 新签名
     *
     * */
    @Override
    public ZhongkaiResult updataName(String wx_uuid, String name) throws Exception {
        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }

        guard.setGuaName(name);

        iBaseDao.update(guard);
        return ZhongkaiResult.ok();
    }


    /***
     * 获取用户信息
     * @param wx_uuid 维修用户uuid
     *
     * */
    @Override
    public ZhongkaiResult getUserData(String wx_uuid) throws Exception {
        if (StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }

        return ZhongkaiResult.ok(guard);
    }


    /***
     * 修改手机号
     * @param wx_uuid 维修用户uuid
     * @param phone 新手机号
     * @param code 验证码
     * */
    @Override
    public ZhongkaiResult updataPhoen(String wx_uuid, String phone, String code) throws Exception {

        if (StringUtils.isNullOrEmpty(wx_uuid) || StringUtils.isNullOrEmpty(phone) || StringUtils.isNullOrEmpty(code)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }


        String jedcode = null;
        try {
            jedcode = jedisClient.get(phone);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "redis连接获取失败");
        }


        if (!code.equals(jedcode)) {
            return ZhongkaiResult.build(400, "验证码错误");
        }

        Guard guard2 = (Guard) iBaseDao.get("from Guard gd where gd.guaPhone='" + phone + "'");

        if (guard2 == null) {
            guard.setGuaPhone(phone);
            iBaseDao.update(guard);

            return ZhongkaiResult.ok();
        }

        if (guard2.getGuaUuid().equals(guard.getGuaUuid())) {
            return ZhongkaiResult.build(500, "您已绑定此手机号");
        } else {
            return ZhongkaiResult.build(500, "此手机已被其他用户绑定");
        }


    }


    /***
     * 修改密码
     * @param wx_uuid 维修用户uuid
     * @param code 验证码
     * @param pass 新密码
     * @param count 方式 1密码  2 验证码

     * */

    @Override
    public ZhongkaiResult updataPass(String wx_uuid, String code, String pass, String count) throws Exception {

        if (StringUtils.isNullOrEmpty(wx_uuid) || StringUtils.isNullOrEmpty(count)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }


        if ("2".equals(count)) {
            String jedcode = null;
            try {
                jedcode = jedisClient.get(guard.getGuaPhone());
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(500, "redis连接获取失败");
            }

            if (StringUtils.isNullOrEmpty(code)) {
                return ZhongkaiResult.build(400, "数据不完整");
            }


            if (!code.equals(jedcode)) {
                return ZhongkaiResult.build(400, "验证码错误");
            }
        }
        if (StringUtils.isNullOrEmpty(pass)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        String strpa = CryptoUtil.encode64(CryptoUtil.SALT, pass);

        guard.setGuaPass(strpa);

        iBaseDao.update(guard);

        return ZhongkaiResult.ok();
    }


    /***
     * 验证密码
     * @param wx_uuid 维修用户uuid
     * @param pass 密码
     *
     * */
    @Override
    public ZhongkaiResult verifyPass(String wx_uuid, String pass) throws Exception {
        if (StringUtils.isNullOrEmpty(wx_uuid) || StringUtils.isNullOrEmpty(pass)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Guard guard;
        guard = (Guard) iBaseDao.get("from Guard gd where gd.guaUuid='" + wx_uuid + "'");

        if (guard == null) {
            return ZhongkaiResult.build(500, "用户不存在");
        }

        String sstr = CryptoUtil.encode64(CryptoUtil.SALT, pass);
        if (!sstr.equals(guard.getGuaPass())) {

            return ZhongkaiResult.build(400, "密码错误");
        }


        return ZhongkaiResult.ok();
    }


    /***
     * 获取设备列表
     * @param wx_uuid 维修用户uuid
     * @param page
     * @param rows
     *
     * */
    @Override
    public ZhongkaiResult getUnitList(String wx_uuid, Integer page, Integer rows) throws Exception {

        if (page == 0 || rows == 0 || StringUtils.isNullOrEmpty(wx_uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(page);
        pageBean.setPageSize(rows);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Anomaly.class);
        detachedCriteria.add(Restrictions.eq("getUuid", wx_uuid));
        detachedCriteria.add(Restrictions.eq("orders", "1"));  //1 已派单 2未派单
/*        detachedCriteria.add(Restrictions.eq("sellRemove", "1")); //增加条件
        detachedCriteria.addOrder(Order.desc("sellTime")); //根据传入的字段进行降序排序*/
        pageBean.setDetachedCriteria(detachedCriteria);
        pageQuerDao.pageQuery(pageBean);

        List<Anomaly> rows1 = pageBean.getRows();
        List<SetUnit> setList = new ArrayList<>();

        for (Anomaly anomaly : rows1) {
            SetUnit setUnit = (SetUnit) iBaseDao.get("from SetUnit su where su.setUuid='" + anomaly.getSetCard() + "'");

            if (setUnit == null){
                continue;
            }else {
                setList.add(setUnit);
                setUnit = null;
            }

        }


        pageBean.setRows(setList);
        return ZhongkaiResult.ok(pageBean);
    }
}
